package com.xcc.common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.xcc.common.mapper.CommonMapper;
import com.xcc.common.mapper.FormInformationMapper;
import com.xcc.common.model.ButtonConfig;
import com.xcc.common.model.CommonObject;
import com.xcc.common.model.CommonTable;
import com.xcc.common.model.FormObject;
import com.xcc.common.model.TableInformation;
import com.xcc.common.model.TableObject;

@Service
public class CommonService {

	@Autowired
	CommonMapper mCommonMapper;
	
	
	@Autowired
	FormInformationMapper mFormInformationMapper;
	
	public Object getCommonObject(String tableId,CommonTable mCommonTable) {
		
		CommonObject mCommonObject=new CommonObject();
		//table 属性
		TableObject mTableObject=new TableObject();
		//form 属性
		FormObject mFormObject=new FormObject();
		
		
		TableInformation  mtableName= mCommonMapper.selectTableName(tableId);
		//分页查询
	//	CommonTable mCommonTable=new CommonTable();
		mCommonTable.setTableName(mtableName.getName());
		
		HashMap<String,Object> mTablePage=new HashMap();
		mTablePage.put("mTablePage",  mCommonMapper.selectPageInfo(mCommonTable,mCommonTable.getmCondition()));
//		mTablePage.put("total",mCommonMapper.execte("select count(*) total from "+mtableName.getName()).get(0).get("total"));
		mTablePage.put("total",   mCommonMapper.selectPageInfoTotal(mCommonTable,mCommonTable.getmCondition()));
		mTableObject.setmTablePage(mTablePage);
		
		//table button 查询
		ButtonConfig mButtonConfig=new ButtonConfig();
		mButtonConfig.setTableName("rm_button_config");
		mButtonConfig.setType("1");
		mButtonConfig.setTableId(tableId);
		mTableObject.setmTableButtonAction(mCommonMapper.selectButtonAction(mButtonConfig));
		mButtonConfig.setType("0");
		//form button
		mFormObject.setmFormButtonAction(mCommonMapper.selectButtonAction(mButtonConfig));
		
		mButtonConfig.setTableName("table_label");
	//	mTableObject.setmTablelabel(mCommonMapper.selectCommon(mButtonConfig));
		mTableObject.setmTablelabel(mCommonMapper.selectTablelabel(mButtonConfig));

		mButtonConfig.setTableName("form_configure");
		List<Map<String, Object>> mformextends=mCommonMapper.selectFormlabel(mButtonConfig);
		for(Map arg:mformextends) {
			if(!StringUtils.isEmpty(arg.get("value"))) {
				arg.put("typeDate", mCommonMapper.execte(arg.get("value")+""));
				arg.put("value","");
			}
		}
		
		
		
		/*HashMap<String,Object> mform=new HashMap();
		 
		mform.put("mFormInfo",mformextends);
		
		mform.put("options",mformextends );*/
		
		mFormObject.setmFormInfo(mformextends);
	
		
		
		
		
		
		mCommonObject.setmFormObject(mFormObject);
		mCommonObject.setmTableObject(mTableObject);
		
		
		
		
		
		return mCommonObject;
	}

	public Object commonquery(String tableId, CommonTable mCommonTable) {
		
		TableInformation  mtableName= mCommonMapper.selectTableName(tableId);
		mCommonTable.setTableName(mtableName.getName());
		//分页查询
	/*	HashMap<String,Object> m=new HashMap<String,Object>();
		m.put("key", "input");
		m.put("value", "el-input");
		mCondition.add(m);*/
		return	mCommonMapper.selectPageInfo(mCommonTable,mCommonTable.getmCondition());
		
	//	return null;
	}

	public void commonSave(String tableId, CommonTable mCommonTable) {
		TableInformation  mtableName= mCommonMapper.selectTableName(tableId);
		mCommonTable.setTableName(mtableName.getName());
		//分页查询
	/*	HashMap<String,Object> m=new HashMap<String,Object>();
		m.put("key", "input");
		m.put("value", "el-input");
		mCondition.add(m);*/
		 	mCommonMapper.commonSave(mCommonTable);
	}

	public void commondelete(String tableId, CommonTable mCommonTable) {
		TableInformation  mtableName= mCommonMapper.selectTableName(tableId);
		mCommonTable.setTableName(mtableName.getName());
		//分页查询
	/*	HashMap<String,Object> m=new HashMap<String,Object>();
		m.put("key", "input");
		m.put("value", "el-input");
		mCondition.add(m);*/
		 	mCommonMapper.commondelete(mCommonTable);
	}

	
	
	
	
	
	
	
	
	
	
	
	
}
